<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class TixianExport implements FromQuery, WithMapping, WithHeadings
{
    use Exportable;

    protected  $model;

    public function __construct($model)
    {
        $this->model = $model;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function query()
    {

        return $this->model->query()->where(function($query){
            $query->with('member', 'bank');
            if(request()->keyword){
                $query->whereHas('member', function($member){
                    $member->where('mobile', 'like', \request()->keyword.'%')->orWhere('nickname','like',\request()->keyword.'%');
                });
            }
            if(!is_null(request()->status)){

                $query->where('status', \request()->status);
            }
        });
    }

    public function map($invoice): array
    {
        return [
            is_null($invoice->member) ? '--' : $invoice->member->nickname,
            is_null($invoice->member) ? '--' : $invoice->member->mobile,
            $invoice->money,
            $invoice->bank->bank_name,
            $this->bankSite()[$invoice->bank->bank_site] ?? '未知',
            ' '.$invoice->bank->bank_card,
            $invoice->bank->bank_address,
            $invoice->bank->bank_location ?? '--',
            $invoice->status == 0 ? '待审核' :  $invoice->status == 1 ? '已通过' : '已拒绝',
            $invoice->created_at,
            $invoice->action_at,
        ];
    }

    public function headings(): array
    {
        return [
            '会员昵称',
            '手机号',
            '金额',
            '开户姓名',
            '开户银行',
            '银行卡号',
            '开户行地址',
            '位置',
            '状态',
            '申请时间',
            '操作时间'
        ];
    }

    public function bankSite()
    {
        return [
            0 => '中国银行',
            1 => '农行',
            2 => '建行',
            3 => '工行',
        ];
    }
}

